<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/4/18
 * Time: 17:31
 */

namespace app\admin\controller;

use app\admin\model\CardModel;
class Card extends Base{
    //添加新车
    public function add(){
        if(request()->isPost()){
            $param = input('post.');
            $model = new CardModel();
            $flag = $model->add($param);
            return json($flag);
        }
        return $this->fetch();
    }
    //编辑
    public function edit(){
        $model = new CardModel();
        if(request()->isPost()){
            $param = input('post.');
            $check=paramCheck($param,'id');
            //if(isset($param['status']) && ($param['status']!=0 && $param['status']!=1 && $param['status']!=-1 )) unset($param['status']);
            if($check['code']!=1) return json($check);
            $flag = $model->edit($param);
            return json($flag);
        }
        $param=input('param.');
        // $model = new CardModel();
        $data = $model->getOne($param['id']);
        $this->assign([
            'data' => $data
        ]);
        return $this->fetch();
    }
    //删除
    public function del(){
        $param=input('param.');
        $model = new CardModel();
        $result = $model->del($param['id']);
        return json($result);
    }
    //下架
    public function down(){
        $param=input('param.');
        $model = new CardModel();
        $result = $model->down($param['id']);
        return json($result);
    }

    //上架
    public function up(){
        $param=input('param.');
        $model = new CardModel();
        $result = $model->up($param['id']);
        return json($result);
    }

    //车辆列表
    public function index(){
        if(request()->isAjax()){
            $param = input('param.');
            $limit = $param['pageSize'];
            $offset = ($param['pageNumber'] - 1) * $limit;
            $where = [];
            if (paramValid($param,'name')) $where['name'] = ['like', '%' . $param['name'] . '%'];
            $model = new CardModel();
            $selectResult = $model->getByWhere($where, $offset, $limit);
            // 拼装参数
            foreach($selectResult as $key=>$vo){
                $selectResult[$key]['thumb']=img($vo['thumb']);
                $selectResult[$key]['addtime']=date('Y-m-d H:i:s',$vo['addtime']);
                $reurl=request()->url();
                if(isset($param['reurl'])) $reurl=$param['reurl'];
                $selectResult[$key]['operate'] = showOperate($this->makeButton($vo['id'],$reurl));
            }
            $return['where'] = $where;  //总数据
            $return['total'] = $model->getAll($where);  //总数据
            $return['rows'] = $selectResult;
            return json($return);
        }
        $param=input('param.');
        $data['sch']=json_encode(array(
            array('id'=>'name','name'=>'产品名称')
        ));
        $data['getUrl']=request()->url();
        $this->assign([
            'data' => $data
        ]);
        return $this->fetch();
    }

    // 上传缩略图
    public function uploadImg()
    {
        if(request()->isAjax()){

            $file = request()->file('file');
            // 移动到框架应用根目录/public/uploads/ 目录下
            $info = $file->move(ROOT_PATH . 'upload/card');
            if($info){
                $src =  '/upload/card' . '/' . date('Ymd') . '/' . $info->getFilename();
                return json(msg(0, ['src' => $src], ''));
            }else{
                // 上传失败获取错误信息
                return json(msg(-1, '', $file->getError()));
            }
        }
    }

    /**
     * 拼装操作按钮
     * @param $id
     * @return array
     */

    private function makeButton($id,$reurl='')
    {
        if($reurl=='') $reurl=request()->url();
        return [
            '编辑' => [
                'auth' => 'card/edit',
                'href' => url('card/edit', ['id' => $id]).'?reurl='.$reurl,
                'btnStyle' => 'primary',
                'icon' => 'fa fa-paste'
            ],
            '删除' => [
                'auth' => 'card/del',
                'href' => "javascript:del(" .$id .")",
                'btnStyle' => 'danger',
                'icon' => 'fa fa-trash-o'
            ]
        ];
    }
}